/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.controller
 * 创建时间：2020年05月04日 11时19分
 **/
package com.zhongwang.mms.module.purchase.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.purchase.model.PInventoryModel;
import com.zhongwang.mms.module.purchase.model.VWProject;
import com.zhongwang.mms.module.purchase.model.adjust.PAdjustModel;
import com.zhongwang.mms.module.purchase.service.PurAdjustService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName： PAdjustController
 * @Description： 库存调整控制层
 * @Author： 徐一贺
 * @Date： 2020/5/4 11:19
 * @Version： V1.0
 **/
@RequestMapping("/purchase/adjust")
@Controller
public class PurAdjustController extends BaseController {
    @Autowired
    private PurAdjustService purAdjustService;


    @GetMapping("/index")
    /**
     * @FunctionName: index
     * @Description: 跳转到调整申请首页
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 7:59
     * @Phone: 18241927380
     * @Param: [model] 渲染数据O型
     * @Return: [String] 页面路径
     * @Version: 1.0.0
     */
    public String index(Model model) {
        model.addAttribute("whiIds", purAdjustService.findWarehouseInfoList());
        return "/purchase/purInvManage/adjustment/apply/page";
    }


    @GetMapping("/create")
    /**
     * @FunctionName: create
     * @Description: 跳转到库存调整申请添加页面
     * @Author: 徐一贺
     * @CreateDate: 2020/5/5 8:01
     * @Phone: 18241927380
     * @Param: [model] 渲染数据O型
     * @Return: [String] 页面路径
     * @Version: 1.0.0
     */
    public String create(Model model) {
        model.addAttribute("whiIds", purAdjustService.findWarehouseInfoList());
        return "/purchase/purInvManage/adjustment/apply/create/page";
    }


    @GetMapping("/review")
    /**
     *@FunctionName: review
     *@Description: 跳转到调整审核页面
     *@Author: 徐一贺
     *@CreateDate: 2020/5/6 7:53
     *@Phone: 18241927380
     *@Param: [model]
     *@Return: java.lang.String
     *@Version: 1.0.0
     */
    public String review(Model model) {
        model.addAttribute("whiIds", purAdjustService.findWarehouseInfoList());
        return "/purchase/purInvManage/adjustment/review/page";
    }

    @PostMapping("/apply/list")
    @ResponseBody
    @RequiresPermissions({"purinv:puradjlist:view"})
    /**
     *@FunctionName: applyList
     *@Description: 获取库存调整申请列表
     *@Author: 徐一贺
     *@CreateDate: 2020/5/4 18:09
     *@Phone: 18241927380
     *@Param: [param] 检索条件
     *@Return: com.zhongwang.mms.common.JsonResult 列表数据
     *@Version: 1.0.0
     */
    public JsonResult applyList(PAdjustModel param) {
        startPage();
        List<PAdjustModel> list = purAdjustService.findApplyList(param);
        return jsonResult(list);
    }

    @PostMapping("/review/list")
    @ResponseBody
    @RequiresPermissions(value={"purinv:puradjlist:view","purinv:puradjapp:view"},logical= Logical.OR)
    /**
     *@FunctionName: reviewList
     *@Description: 获取库存调整审核列表
     *@Author: 徐一贺
     *@CreateDate: 2020/5/4 18:09
     *@Phone: 18241927380
     *@Param: [param] 检索条件
     *@Return: com.zhongwang.mms.common.JsonResult 列表数据
     *@Version: 1.0.0
     */
    public JsonResult reviewList(PAdjustModel param) {
        startPage();
        List<PAdjustModel> list = purAdjustService.findReviewList(param);
        return jsonResult(list);
    }

    @PostMapping("/review/adopt")
    @ResponseBody
    @RequiresPermissions({"purinv:puradjapp:edit"})
    /**
     *@FunctionName: reviewAdopt
     *@Description: 审核通过
     *@Author: 徐一贺
     *@CreateDate: 2020/5/6 10:37
     *@Phone: 18241927380
     *@Param: [param] 主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult reviewAdopt(PAdjustModel param) {
        return purAdjustService.reviewAdopt(param);
    }
    @PostMapping("/review/adopts")
    @ResponseBody
    @RequiresPermissions({"purinv:puradjapp:edit"})
    /**
     *@FunctionName: reviewAdopts
     *@Description: 批量审核通过
     *@Author: 徐一贺
     *@CreateDate: 2020/5/6 10:37
     *@Phone: 18241927380
     *@Param: [param] 主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult reviewAdopts(PAdjustModel param) {
        return purAdjustService.reviewAdopts(param);
    }

    @PostMapping("/review/fail")
    @ResponseBody
    @RequiresPermissions({"purinv:puradjapp:edit"})
    /**
     *@FunctionName: reviewFail
     *@Description: 审核不通过
     *@Author: 徐一贺
     *@CreateDate: 2020/5/6 15:00
     *@Phone: 18241927380
     *@Param: [param] 主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult reviewFail(PAdjustModel param) {
        return purAdjustService.reviewFail(param);
    }

    @PostMapping("/apply/invList")
    @ResponseBody
    /**
     *@FunctionName: applyInvList
     *@Description: 获取库存列表
     *@Author: 徐一贺
     *@CreateDate: 2020/5/5 8:08
     *@Phone: 18241927380
     *@Param: [param] 查询条件
     *@Return: com.zhongwang.mms.common.JsonResult 库存列表
     *@Version: 1.0.0
     */
    public JsonResult applyInvList(PInventoryModel param) {
        startPage();
        List<PInventoryModel> list = purAdjustService.findPInventoryModelList(param);
        return jsonResult(list);
    }

    @PostMapping("/apply/getInventory")
    @ResponseBody
    /**
     *@FunctionName: applyGetInventory
     *@Description: 根据主键获取库存信息
     *@Author: 徐一贺
     *@CreateDate: 2020/5/5 8:48
     *@Phone: 18241927380
     *@Param: [param.ityId] 主键
     *@Return: [com.zhongwang.mms.common.JsonResult.data] 库存信息
     *@Version: 1.0.0
     */
    public JsonResult applyGetInventory(PInventoryModel param) {
        return JsonResult.success("", purAdjustService.getInventoryByPrimaryKey(param.getItyId()));
    }

    @PostMapping("/apply/remove")
    @ResponseBody
    @RequiresPermissions({"purinv:puradjlist:del"})
    /**
     *@FunctionName: applyRemove
     *@Description: 跟库主键删除（逻辑）调整申请
     *@Author: 徐一贺
     *@CreateDate: 2020/5/4 20:38
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult applyRemove(PAdjustModel param) {
        return purAdjustService.applyRemove(param);
    }

    @PostMapping("/apply/create")
    @ResponseBody
    @RequiresPermissions({"purinv:puradjlist:edit"})
    /**
     *@FunctionName: applyCreate
     *@Description: 创建调整申请
     *@Author: 徐一贺
     *@CreateDate: 2020/5/5 9:47
     *@Phone: 18241927380
     *@Param: [param] 调整申请
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult applyCreate(PAdjustModel param) {
        return purAdjustService.applyCreate(param);
    }

    @PostMapping("/apply/getAdjust")
    @ResponseBody
    /**
     *@FunctionName: applyGetAdjust
     *@Description: 根据主键获取申请
     *@Author: 徐一贺
     *@CreateDate: 2020/5/5 9:47
     *@Phone: 18241927380
     *@Param: [param] 主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult applyGetAdjust(PAdjustModel param) {
        return JsonResult.success("", purAdjustService.applyGetAdjust(param));
    }

    @PostMapping("/apply/saveAdjust")
    @ResponseBody
    @RequiresPermissions({"purinv:puradjlist:edit"})
    /**
     *@FunctionName: applySaveAdjust
     *@Description: 保存调整申请
     *@Author: 徐一贺
     *@CreateDate: 2020/5/5 15:02
     *@Phone: 18241927380
     *@Param: [param] 主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult applySaveAdjust(PAdjustModel param) {
        return purAdjustService.applySaveAdjust(param);
    }

    @PostMapping("/apply/submit")
    @ResponseBody
    @RequiresPermissions({"purinv:puradjlist:edit"})
    /**
     *@FunctionName: applySubmit
     *@Description: 调整申请提交
     *@Author: 徐一贺
     *@CreateDate: 2020/5/5 16:08
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult applySubmit(PAdjustModel param) {
        return purAdjustService.applySubmit(param);
    }

    @PostMapping("/apply/getCompareAdjust")
    @ResponseBody
    /**
     *@FunctionName: applyGetCompareAdjust
     *@Description: 根据主键和父级依赖获取比较显示实体类
     *@Author: 徐一贺
     *@CreateDate: 2020/5/5 16:31
     *@Phone: 18241927380
     *@Param: [param] 主键 父级
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult applyGetCompareAdjust(PAdjustModel param) {
        return JsonResult.success("", purAdjustService.applyGetCompareAdjust(param));
    }

    @PostMapping("/codeExist")
    @ResponseBody
    /**
     *@FunctionName: codeExist
     *@Description: 验证项目号是否存在
     *@Author: 徐一贺
     *@CreateDate: 2020/5/5 16:08
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: java.lang.String
     *@Version: 1.0.0
     */
    public String codeExist(VWProject param) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", purAdjustService.codeExist(param));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }

}